package com.example.springbootvuetest.controller;

import com.example.springbootvuetest.Utils.IdFactoryUtil;
import com.example.springbootvuetest.Utils.OrderTaskHandler;
import com.example.springbootvuetest.base.ResultCode;
import com.example.springbootvuetest.mapper.IdleItemMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springbootvuetest.base.Result;
import lombok.RequiredArgsConstructor;
import com.example.springbootvuetest.entity.Order;
import com.example.springbootvuetest.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

import static com.example.springbootvuetest.Utils.OrderTaskHandler.orderService;


/**
 * <p>
 * controller
 * </p>
 *
 * @author shuoye
 * @since 2023-03-14
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@Api(tags = "REST - ")
@RestController
@RequestMapping("/order")
public class OrderController {
    private final IOrderService service;

    @PostMapping("/add")
    @ApiOperation(value = "新增")
    public Result createOne(@RequestBody Order order) {
        if (OrderTaskHandler.orderService == null) {
            OrderTaskHandler.orderService = service;
        }
        order.setOrderNumber(IdFactoryUtil.getOrderId());
        order.setUserId(order.getUserId());
        order.setOrderStatus((byte) 0);
        order.setPaymentStatus((byte) 0);
        return service.addOrder(order) ?
                Result.success("订单成功,请在三十分钟内支付", order) :
                Result.error(ResultCode.SYS_ERROR);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除")
    public Result delete(@PathVariable("id") Integer id) {
        boolean flag = service.removeById(id);
        return flag ? Result.success() : Result.error();
    }

    @GetMapping("/info")
    @ApiOperation(value = "查看订单信息")
    public Result queryOrderInfo(@RequestParam("userId") Integer userId, @RequestParam(name = "id") Integer orderId) {
        Order order = service.getOrder(orderId);
        if (order.getUserId().equals(userId) ||
                order.getIdleItem().getUserId().equals(userId)) {
            return Result.success(order);
        }
        return Result.error();
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改订单状态")
    public Result update(@RequestBody Order order) {
        if (order.getPaymentStatus() != null && order.getPaymentStatus().equals((byte) 1)) {
            order.setPaymentTime(new Date());
        }
        if (service.updateOrder(order)) {
            return Result.success(order);
        }
        return Result.error(ResultCode.SYS_ERROR);
    }

    @GetMapping("/my-sold")
    @ApiOperation(value = "查看我收到的订单")
    public Result getMySold(@RequestParam(name = "userId") Integer userId) {
        return Result.success(service.getMySoldIdle(userId));
    }

    @GetMapping("/my")
    @ApiOperation(value = "查看我收到的订单")
    public Result getMyOrder(@RequestParam(name = "userId") Integer userId) {
        return Result.success(service.getMyOrder(userId));
    }
}